package com.frxuexi.park.view

import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.paging.filter
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.ItemTouchHelper.LEFT
import androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.park.OnItemClickListener
import com.frxuexi.park.OnItemLongClickListener
import com.frxuexi.park.ParkAdapter
import com.frxuexi.park.ParkViewModel
import com.frxuexi.park.R
import com.frxuexi.park.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/park/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding, ParkViewModel>() {
    val parkAdapter by lazy { ParkAdapter() }
    var type="类型"
    var para=""
    lateinit var datas:PagingData<ParkBeanItem>
    override fun initData() {
        vm.deleteParkData.observe(this){
            if(it){
                lifecycleScope.launch {
                    vm.getParkByType("标准车位",type).collect{
                        datas=it
                        parkAdapter.submitData(it)
                    }
                }
            }
        }
    }

    override fun initView() {
        binding.headPark.headerBack.setOnClickListener {
            finish()
        }
        binding.headPark.headerTitle.setText("车位管理")
        binding.headPark.headerAdd.setOnClickListener {
            ARouter.getInstance().build("/park/ParkAddActivity").navigation()
        }

        binding.parkRv.apply {
            adapter=parkAdapter
            layoutManager=LinearLayoutManager(this@ParkActivity)
        }

        parkAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(item: ParkBeanItem) {
                ARouter.getInstance().build("/park/ParkEditActivity").withString("pid",item.id.toString()).navigation()
            }

        })
        parkAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(): Boolean {
                ARouter.getInstance().build("/park/ParkVRActivity").navigation()
               return true
            }

        })


        val helper = ItemTouchHelper(object : SimpleCallback(0, LEFT) {
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
//                val pid =
//                    viewHolder.itemView.findViewById<TextView>(R.id.park_id).text.toString()
                vm.deletePark(parkAdapter.snapshot().items.get(position).id!!)
            }

        })
       helper.attachToRecyclerView(binding.parkRv)

        binding.parkFilter.setOnClickListener {
            binding.parkDrawer.openDrawer(GravityCompat.END)
        }

        binding.parkRgstatus.setOnCheckedChangeListener { group, checkedId ->
            type="状态"
            var para=""
            when(checkedId){
                R.id.rb_statuskx ->
                    para="空闲"
                R.id.rb_stauszy ->
                    para="已占用"
                else->
                    para="维修中"
            }
            binding.parkDrawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                vm.getParkByType(para,type).collect{
                    parkAdapter.submitData(it)
                }
            }

        }

        binding.parkSv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                para=query!!
                lifecycleScope.launch {
                    if(type=="区域") {
                        vm.getParkByType(query!!, type).collect {
                            parkAdapter.submitData(it)
                        }
                    }else{
                        parkAdapter.submitData(datas.filter {
                            it.licensePlate == para
                        })
                    }
                }

                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }

        })

        binding.parkArea.setOnClickListener {
            binding.parkDrawer.closeDrawer(GravityCompat.END)
            type="区域"
            if(para.isNotEmpty()){
                lifecycleScope.launch {
                    vm.getParkByType(para,type).collect{
                        parkAdapter.submitData(it)
                    }
                }
            }

        }

        binding.parkCarNo.setOnClickListener {
            type="车牌号"
            lifecycleScope.launch {
                parkAdapter.submitData(datas.filter {
                    it.licensePlate == para
                })
            }
        }

        binding.parkRgtype.setOnCheckedChangeListener { group, checkedId ->
            binding.parkDrawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                if(checkedId== R.id.rb_typea){
                    vm.getParkByType("标准车位",type).collect{
                        parkAdapter.submitData(it)
                    }
                }else{
                    vm.getParkByType("VIP车位",type).collect{
                        parkAdapter.submitData(it)
                    }
                }
            }

        }
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            vm.getParkByType("标准车位",type).collect{
                datas=it
                parkAdapter.submitData(it)
            }
        }
    }

}